package com.youxin.dataSet;


import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class DataSetJobLambda {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSet<String> lines = env.readTextFile("C:\\data\\input\\words", "UTF-8");
        FlatMapOperator<String, Tuple2<String, Long>> wordAndOne = lines.flatMap((String line, Collector<Tuple2<String, Long>> collector) -> {
            String[] split = line.split(" ");
            for (String word : split) {
                collector.collect(new Tuple2<String, Long>(word, 1L));
            }
        }).returns(Types.TUPLE(Types.STRING,Types.LONG));// 这里面类型推断不出来，要强制的写上

        AggregateOperator<Tuple2<String, Long>> sum = wordAndOne.groupBy(0).sum(1);

        try {
            sum.print();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用print的时候可以不用调用execute了
        //env.execute("streaming job");

    }
}
